Write a program to check if two strings are anagrams of each other.
Write a program to check if two strings are anagrams of each other.
244
19-Apr-2023
Aryan Kumar
24-Apr-2023In this program, we define a method called areAnagrams that takes two strings as arguments and returns a boolean indicating whether or not they are anagrams of each other. The method first removes all whitespace from both strings and converts them to lowercase. Then it checks if the two strings are of the same length; if they are not, they cannot be anagrams. Finally, it converts each string to a character array, sorts the arrays, and compares them using the Arrays.equals method. If the sorted arrays are equal, the strings are anagrams; otherwise, they are not.
In the main method, we define two strings (str1 and str2) and call the areAnagrams method with those strings. If the method returns true, we print a message indicating that the strings are anagrams; otherwise, we print a message indicating that they are not.